package project.android;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


public class ProjectActivity extends ListActivity{
	
	 
    @Override
    public void onCreate(Bundle savedInstanceState) { 
		
    	super.onCreate(savedInstanceState);
    	final String[] LIST =   new String[] {"My Tasks" , "My Groups"};
    	setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item, LIST)); 
        
    	//Connect to database to check if the user has an account
        final DatabaseConnection db = new DatabaseConnection(this);
        //db.connectDatabase("TaskBoard","Authentication");
        db.addTableIfNotExists("TaskBoard","Authentication","ID INTEGER PRIMARY KEY,USERNAME VARCHAR,PASSWORD VARCHAR");
        Cursor c= db.selectAll();
        int isAuthentized = c.getCount();
       
        //In case the user doesn't have an account, prompt an alert box to take username and password
        if (isAuthentized == 0){
        	
	        LayoutInflater factory = LayoutInflater.from(ProjectActivity.this);
	        final View input = factory.inflate(R.layout.login_alert_box, null);
	        new AlertDialog.Builder(ProjectActivity.this)
	        	.setTitle("Signup")
	        	.setView(input)
	        	.setPositiveButton("Save", 
	        		new DialogInterface.OnClickListener() {
			        	public void onClick(DialogInterface dialog, int whichButton){
				            
			        		Synchronized.username = ((EditText) input.findViewById(R.id.userNameIn)).getText().toString();
			            	Synchronized.password = ((EditText) input.findViewById(R.id.passwordIn)).getText().toString();
			            	
			        		String toBeInserted =Synchronized.username + "," +Synchronized.password;
			        		
			        		db.connectDatabase("TaskBoard","Authentication");
				            db.insert(toBeInserted);
				            db.close();
				         
			        	} 	
	        	})
		        .setNeutralButton(android.R.string.cancel,
		            new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int whichButton){
			        	   
			        	   finish();
			           
			           }
		        })
		        .show();
	        
	        //Send account info to server
	        //Synchronized.syncTasks(db);
	        
        }
        else{
        	
        	Synchronized.username = c.getString(c.getColumnIndex("USERNAME"));
        	Synchronized.password = c.getString(c.getColumnIndex("PASSWORD"));
        	
        }
        
        db.close();
        
        //Set list layout
        ListView lv = getListView(); 
        lv.setTextFilterEnabled(true); 
        lv.setOnItemClickListener(new OnItemClickListener() { 
        	public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
        		Intent i;
        		if(position == 0)
        			i = new Intent(ProjectActivity.this, ManageTasks.class);
        		else
        			i = new Intent(ProjectActivity.this, ManageGroups.class);
        		startActivity(i);
        	
        	} 
        });
        
        
    }
    
	
}



